package com.hfjyz.exam.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.hfjyz.exam.command.AdminLoginCommand;
import com.hfjyz.exam.service.LoginService;

public class AdminLoginController extends SimpleFormController {
	private LoginService loginService;

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		AdminLoginCommand cmd = (AdminLoginCommand) command;
		if (!errors.hasErrors()) {
			try {
				loginService.adminLogin(request, cmd, errors);
			} catch (Exception ex) {
				throw ex;
			}
		}
		if (errors.hasErrors()) {
			return new ModelAndView(getFormView(), errors.getModel());
		}
		request.getSession().setAttribute("AdminInfo", cmd);
		return new ModelAndView(getSuccessView(), errors.getModel());
	}

	/**
	 * @return the loginService
	 */
	public LoginService getLoginService() {
		return loginService;
	}

	/**
	 * @param loginService
	 *            the loginService to set
	 */
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
}
